package Snackbarsystem;

import java.io.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class managerFood {
    public void show() throws Exception {
        ArrayList<Food> f = new ArrayList<Food>();
        Food f1 = new Food(01, "榴莲酥", 5, "咸甜");
        Food f2 = new Food(02, "西瓜捞", 8, "凉甜");
        Food f3 = new Food(03, "蛋焗南瓜", 10, "鲜甜");
        f.add(f1);
        f.add(f2);
        f.add(f3);
        try (
                BufferedWriter in0=new BufferedWriter(new FileWriter("D:\\idea java\\Foodsystem\\src\\File\\food1.txt"));
        ){
            for (int i=0;i<f.size();i++) {
                String allFood = f.get(i).toString();
                in0.write(allFood);
                in0.flush();
            }
        }
        while (true)//无限循环
        {
            System.out.println("------小吃店管理系统-----");
            System.out.println("1.添加小吃");
            System.out.println("2.删除小吃");
            System.out.println("3.更改小吃");
            System.out.println("4.查看小吃");
            System.out.println("5.更改密码");
            System.out.println("6.查看用户");
            System.out.println("7.删除用户");
            System.out.println("8.查看、更改用户订单");
            System.out.println("9.退出");
            System.out.println("请选择系统功能：");
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            if (n == 1) {
                addFood(f);
            } else if (n == 2) {
                deleteFood(f);
            } else if (n == 3) {
                changeFood(f);
            } else if (n == 4) {
                checkFood(f);
            }else if (n==5){
                changepassWord();
            } else if (n==6){
                checkUser();
            }else if(n==7){
                deleteUser();
            } else if (n==8){
                checkorder();
            }
            else if (n == 9) {
                System.out.println("谢谢使用小京系统");
                break;
            }
            else{
                System.out.println("无此功能，请重新输入！");
            }
        }
    }
    public static void addFood(ArrayList<Food> f) throws Exception {
        System.out.println("----欢迎添加小吃----");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入小吃编号：");
        int id = sc.nextInt();
        boolean flag = true;
        for (int i = 0; i < f.size(); i++) {
            Food a = f.get(i);//遍历数组列表
            if (a.getId() == id) {
                flag = false;
                System.out.println("该小吃已存在");
                break;
            }
        }
        if (flag) {
            System.out.println("请输入小吃名字：");
            String Name = sc.next();
            System.out.println("请输入小吃价格");
            int Price = sc.nextInt();
            System.out.println("请输入小吃口味");
            String Taste = sc.next();
            Food fo = new Food();
            fo.setId(id);
            fo.setName(Name);
            fo.setPrice(Price);
            fo.setTaste(Taste);
            f.add(fo);
            try (
                    BufferedWriter in1 = new BufferedWriter(new FileWriter("D:\\idea java\\Foodsystem\\src\\File\\food1.txt", true));
            ) {
                PrintWriter fw = new PrintWriter(in1);
                fw.print(fo);
                fw.flush();
            }
            System.out.println("添加成功！");
        }
    }

    public static void deleteFood(ArrayList<Food> f) throws Exception {
        System.out.println("----欢迎删除小吃----");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的小吃编号");
        int num = sc.nextInt();
        int all = -1;//创建索引，找数组列表中以有数据
        for (int i = 0; i < f.size(); i++) {
            Food food = f.get(i);//遍历数组列表
            if (food.getId() == num) //判断改小吃是否存在
            {
                all = i;
                break;
            }
        }
        if (all == -1) {
            System.out.println("无此小吃，请重新选择");
        } else {
            f.remove(all);
            try(
                    BufferedWriter in2 = new BufferedWriter(new FileWriter("D:\\idea java\\Foodsystem\\src\\File\\food1.txt"))
            ) {
                for (int i = 0; i < f.size(); i++) {
                    String defood;
                    defood = f.get(i).toString();
                    in2.write(defood);
                    in2.flush();
                }
            }
            System.out.println("删除小吃成功");
        }
    }

    public static void changeFood(ArrayList<Food> f) throws Exception {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要修改的小吃编号");
        int id = sc.nextInt();
        int all = -1;//创建索引，找数组列表中以有数据
        for (int i = 0; i < f.size(); i++) {
            Food fo = f.get(i);
            if (fo.getId() == id)//判断改小吃是否存在
            {
                all = 1;
                break;
            }
        }
        if (all == -1) {
            System.out.println("您要改变的小吃不存在");
        } else {
            System.out.println("请输入新小吃名字：");
            String Name = sc.next();
            System.out.println("请输入新小吃价格");
            int Price = sc.nextInt();
            System.out.println("请输入新小吃口味");
            String Taste = sc.next();
            Food fo = new Food();
            fo.setId(id);
            fo.setName(Name);
            fo.setPrice(Price);
            fo.setTaste(Taste);
            f.set(id - 1, fo);//将已完善的小吃在数组列表中创建
            try(
                    BufferedWriter in3=new BufferedWriter(new FileWriter("D:\\idea java\\Foodsystem\\src\\File\\food1.txt"));
                    ){
                for (int i = 0; i < f.size(); i++){
                    String  changeFood;
                            changeFood = f.get(i).toString();
                    in3.write(changeFood);
                    in3.flush();
                }
            }
            System.out.println("修改小吃信息成功");
        }
    }

    public static void checkFood(ArrayList<Food> f) throws Exception {
        if (f.size() == 0) {
            System.out.println("暂无小吃信息");
            return;//停止程序运行下去
        }
        System.out.println("----欢迎查看小吃----");
        System.out.println("编号:\t小吃名:\t   价格:\t    口味:");
        try (
                BufferedReader out = new BufferedReader(new FileReader("D:\\idea java\\Foodsystem\\src\\File\\food1.txt"));
        ) {
            String check;
            while ((check = out.readLine()) != null) {
                System.out.println(check);
            }
        }
    }
    public static void changepassWord()throws Exception{
        List<String> pwo=new ArrayList<String>();
        LinkedList<String> nwo=new LinkedList<String>();
        try (
                BufferedReader br0=new BufferedReader(new FileReader("D:\\idea java\\Foodsystem\\src\\File\\passWord.txt"));
                BufferedReader br1=new BufferedReader(new FileReader("D:\\idea java\\Foodsystem\\src\\File\\passWord.txt"));
                ) {
            int co = 0;
            while ((br0.readLine()) != null)
            {
                co++;
            }
            String n= Integer.toString(co);
            pwo.add(n);
            Scanner sc=new Scanner(System.in);
            System.out.println("请输入旧密码：");
            String oldpassword=sc.next();
            for (int i=1;i<=co;i++){
                String old=br1.readLine();
                if (old.equals(oldpassword)){
                   String ma="yes";
                   pwo.add(ma);
                   break;
                }else {
                    String ma="no";
                    pwo.add(ma);
                    break;
                }
            }
        }
        String match= pwo.get(1);
        if (match=="yes") {
            try (
                    BufferedReader br = new BufferedReader(new FileReader("D:\\idea java\\Foodsystem\\src\\File\\passWord.txt"));
            ) {
                Scanner nw = new Scanner(System.in);
                System.out.println("请输入新密码：");
                String newword = nw.next();
                String m = pwo.get(0);
                int x = Integer.parseInt(m);
                for (int i = 1; i <= x; i++) {
                    nwo.add(br.readLine());
                }
                nwo.remove(0);
                nwo.addFirst(newword);
            }
                try (
                        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\idea java\\Foodsystem\\src\\File\\passWord.txt"));
                ) {
                    for (int j = 0; j < nwo.size(); j++) {
                        String word = nwo.get(j);
                        bw.write(word + "\n");
                    }
                    bw.flush();
                }
                System.out.println("修改成功！");
        }
        else if(match=="no") {
            System.out.println("旧密码错误！");
        }
    }
    public static void checkUser()throws Exception{
        List<String> look1=new ArrayList<String>();
        List<String> look2=new ArrayList<String>();
        try(
                BufferedReader out0=new BufferedReader(new FileReader("D:\\idea java\\Foodsystem\\src\\File\\Account.txt"));
                BufferedReader out1=new BufferedReader(new FileReader("D:\\idea java\\Foodsystem\\src\\File\\Account.txt"));
                BufferedReader out2=new BufferedReader(new FileReader("D:\\idea java\\Foodsystem\\src\\File\\passWord.txt"))

        ){
            while ((out0.readLine())!=null){
                look1.add(out1.readLine());
                look2.add(out2.readLine());
            }
            look1.remove(0);
            look2.remove(0);
            for (int i=0;i<look1.size();i++){
                String a=look1.get(i);
                System.out.println("账号："+a);
                String b=look2.get(i);
                System.out.println("密码："+b);
                }
            }
        }
        public static void deleteUser()throws Exception{
        List<String> mediuma=new ArrayList<String>();//储存账户
            List<String> mediump=new ArrayList<String>();//储存密码
            List<String> jud=new ArrayList<String>();//储存判断是否有对应账号
        try(    BufferedReader out0=new BufferedReader(new FileReader("D:\\idea java\\Foodsystem\\src\\File\\Account.txt"));
                BufferedReader out1=new BufferedReader(new FileReader("D:\\idea java\\Foodsystem\\src\\File\\Account.txt"));
                 BufferedReader out2=new BufferedReader(new FileReader("D:\\idea java\\Foodsystem\\src\\File\\passWord.txt"))
                ) {
            while ((out0.readLine()) != null) {
                String ac = out1.readLine();
                mediuma.add(ac);
                String ps = out2.readLine();
                mediump.add(ps);
            }
        }
                for(int j=1;j<mediuma.size();j++){
                    System.out.println("账号："+mediuma.get(j)+"\n");
                    System.out.println("密码："+mediump.get(j)+"\n");
                }
                Scanner sc=new Scanner(System.in);
            System.out.println("请输入您要删除的账号：");
            String da=sc.next();
            for (int i=1;i<mediuma.size();i++){
                if (da.equals(mediuma.get(i))){
                    mediuma.remove(i);
                    mediump.remove(i);
                    jud.add("yes");
                    break;
                }
            }
            if (jud.isEmpty()==false) {
                try (
                        BufferedWriter in1 = new BufferedWriter(new FileWriter("D:\\idea java\\Foodsystem\\src\\File\\Account.txt"));
                        BufferedWriter in2 = new BufferedWriter(new FileWriter("D:\\idea java\\Foodsystem\\src\\File\\passWord.txt"));
                ) {
                    for (int i = 0; i < mediuma.size(); i++) {
                        in1.write(mediuma.get(i) + "\n");
                        in1.flush();
                        in2.write(mediump.get(i) + "\n");
                        in2.flush();
                    }
                    System.out.println("删除成功！");
                }
            }
            else {
                System.out.println("账户不存在！");
            }
        }
    public static void checkorder()throws Exception {
        List<String> so=new ArrayList<String>();//储存订单
        List<String> ao=new ArrayList<String>();//储存总菜单
        try(
                BufferedReader ro=new BufferedReader(new FileReader("D:\\idea java\\Foodsystem\\src\\File\\order.txt"));
                BufferedReader ao_1 =new BufferedReader (new FileReader("D:\\idea java\\Foodsystem\\src\\File\\food1.txt"));
        ) {
            String check;
            while ((check = ao_1.readLine()) != null) {
                ao.add(check);//将所有小吃存入菜单
            }

            String reorder;
            int jilu = 0;
            System.out.println("-用户订单如下-");
            System.out.println("编号:\t小吃名:\t   价格:\t    口味:");
            while ((reorder = ro.readLine()) != null) {
                so.add(reorder);
                jilu++;
                System.out.println(reorder);//输出每一行order中小吃
            }

            System.out.println("--管理订单--");
            System.out.println("请选择功能:" + "\n" + "1.添加    2.删除    3.更换   4.确认");
            Scanner number = new Scanner(System.in);
            String choice = number.next();

            if (choice.equals("2")) {
                System.out.println("请选择删除第几个小吃:");
                int num_1 = number.nextInt();
                if (num_1 <= jilu) {
                    so.remove(num_1 - 1);
                    System.out.println("删除成功!");
                }else {
                    System.out.println("无此小吃!");
                }
            } else if (choice.equals("1")) {
                System.out.println("请输入添加小吃编号:");
                int num_2 = number.nextInt();
                so.add(ao.get(num_2 - 1));
                System.out.println("添加成功!");
            } else if (choice.equals("3")) {
                System.out.println("请输入要跟换第几个小吃:");
                int n = number.nextInt();
                if (n<=jilu){
                    System.out.println("请输入想跟换的小吃编号:");
                    int num_3 = number.nextInt();
                    so.set(n - 1, ao.get(num_3 - 1));
                    System.out.println("跟换成功!");
                }else{
                    System.out.println("无此小吃!");
                }
            } else if (choice.equals("4")) {
                System.out.println("已确认订单!");
                for (int i=0;i<so.size();i++){
                    so.remove(i);
                }
                try(
                        BufferedWriter clear = new BufferedWriter(new FileWriter("D:\\idea java\\Foodsystem\\src\\File\\order.txt"));
                ){
                    clear.write("订单无内容!");
                    clear.flush();
                }
            } else {
                System.out.println("无此功能!");
                if (so.isEmpty() != false) {
                    for (int i = 0; i < so.size(); i++) {
                        so.remove(i);
                    }
                }
            }
        }
        if (so.isEmpty()==false) {
            try (
                    BufferedWriter co = new BufferedWriter(new FileWriter("D:\\idea java\\Foodsystem\\src\\File\\order.txt"));
            ) {
                for (int i = 0; i < so.size(); i++) {
                    co.write(so.get(i) + "\n");
                }
                co.flush();
            }
        }
    }
}
